package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.cp.api.CadUserPwdmodifyCmd;
import com.jackrain.nea.fc.cp.adutils.AdUser;
import com.jackrain.nea.fc.cp.mapper.CadorgMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.HashMap;

/**
 * Created by ZCY on 2017/12/17.
 * 修改AD域用户密码
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "cp")
public class CadUserPwdModifyCmdImpl extends CommandAdapter implements CadUserPwdmodifyCmd {
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH-mm-ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (param == null) {
            throw new NDSException("请选择需要修改密码的用户！");
        }
        CadorgMapper cadorgMapper = ApplicationContextHandle.getBean(CadorgMapper.class);
        JSONArray Idsary = param.getJSONArray("IDS");
        Boolean idsbol = Idsary == null || (Idsary != null && Idsary.size() == 0);
        if (idsbol) {
            throw new NDSException("请传入用户IDS数组");
        }
        JSONArray dataary = new JSONArray();
        AdUser.ldapload();
        Boolean backbol = true;
        for (int i = 0; i < Idsary.size(); i++) {
            Long objid = Idsary.getLong(i);
            String cn = null;
            try {
                JSONObject usershash = cadorgMapper.findhuseratu(objid);
                if (usershash == null) {
                    JSONObject cnullobj = new JSONObject(true);
                    cnullobj.put("org", "ID=" + objid + " 没有ADYU=Y的数据");
                    dataary.add(cnullobj);
                } else {
                    cn = TypeUtils.castToString(usershash.get("cn"));
                    if (StringUtils.isEmpty(cn)) {
                        throw new NDSException("ID=" + objid + "的用户名为空");
                    }
                    String unicodePwd = TypeUtils.castToString(usershash.get("unicodePwd"));
                    if (StringUtils.isEmpty(unicodePwd)) {
                        throw new NDSException(cn + "该用户数据库中密码为空");
                    }
                    String Isenabled = TypeUtils.castToString(usershash.get("ISENABLED"));
                    if ("1".equals(Isenabled)) {
                        JSONObject huserobj = new JSONObject(true);
                        huserobj.put("cn", cn);
                        huserobj.put("unicodePwd", unicodePwd);
                        huserobj.put("sAMAccountName", cn);
                        String orgurl = AdUser.modifypwd(huserobj);
                        JSONObject successtobj = new JSONObject(true);
                        successtobj.put("org", cn + "所在的组织路径:" + orgurl);
                        dataary.add(successtobj);
                    } else {
                        JSONObject huserenableobj = new JSONObject(true);
                        huserenableobj.put("cn", cn);
                        huserenableobj.put("unicodePwd", unicodePwd);
                        huserenableobj.put("sAMAccountName", cn);
                        String orgenableurl = AdUser.modifyenablepwd(huserenableobj);
                        JSONObject successtobj = new JSONObject(true);
                        successtobj.put("org", cn + "在数据库中是不启用，在AD域所在的组织路径:" + orgenableurl);
                        dataary.add(successtobj);
                    }
                }

            } catch (Exception et) {
                JSONObject exobj = new JSONObject(true);
                exobj.put("org", cn);
                exobj.put("error", et.getMessage());
                dataary.add(exobj);
                backbol = false;
            }

        }
        if (backbol) {
            vh.put("code", 0);

        } else {
            vh.put("code", -1);
        }
        vh.put("message", dataary);
        AdUser.closeCtx();
        return vh;
    }
}
